// packageG/mycomponent/cloud-shop/cloud_shop_popup/cloud_shop_popup.js
const app = getApp();
Component({

  /**
   * 组件的属性列表
   */
  properties: {
    component_key: {
      type: null
    },
    datas: {
      type: null
    },
    component_id: {
      type: null
    },
    page_id: {
      type: null
    },
    index: {
      type: null
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    newAccount: false,
    openStore: false,
    newAccountShop: false,
    newAccountTask: false,
    self_reward_name: "销售佣金",
    cloud_shop_info: [],
    isshow: false,
    iPnoneBottomBol: false,
    nowTime: parseInt(new Date().getTime() / 1000),
  },
  lifetimes: {
    attached() {
      this.getiPnoneBottom(); // 获取是否需要iponex底部安全区
      let language = wx.getStorageSync("langIndex");
      let yz_basic_info = wx.getStorageSync('yz_basic_info');
      if (!yz_basic_info.plugin_setting?.cloud_shop) return;
      let shop_level_id = yz_basic_info.plugin_setting?.cloud_shop.shop_level_id;
      let is_base = yz_basic_info.plugin_setting?.cloud_shop.is_base;
      let self_reward_name = yz_basic_info.plugin_setting?.cloud_shop.self_reward_name;
      let first_order_status = yz_basic_info.plugin_setting?.cloud_shop.first_order_status;
      let wechat_follow = yz_basic_info.plugin_setting?.cloud_shop.wechat_follow;
      let master_status = yz_basic_info.plugin_setting?.cloud_shop.master_status;
      this.setData({
        language: language.en,
        self_reward_name,
        cloud_shop_info: yz_basic_info.plugin_setting.cloud_shop
      });
      if (this.data.component_key == 'U_NComerUpgrade') {
        if (is_base && (!first_order_status || !wechat_follow)) {
          // 实习店长才显示
          this.setData({
            newAccountTask: true,
          })
          this.replaceText();
          return;
        }
      }
      if (this.data.component_key == 'U_NComerZone') {
        if (this.data.datas.permissions && shop_level_id == this.data.datas.permissions) {
          this.get_shop_reward();
          return
        }
      }
      if (this.data.component_key != 'U_NComerZone' && this.data.component_key != 'U_NComerUpgrade' && is_base && !wechat_follow && master_status) {
        this.setData({
          openStore: true
        })
      }
    }
  },
  /**
   * 组件的方法列表
   */
  methods: {
    getiPnoneBottom() {
      try {
        var value = wx.getStorageSync("iPhoneBottom");
        if (value) {
          this.setData({
            iPnoneBottomBol: true,
          });
      }
      } catch (e) {}
    },
    getPoster() {
      this.setData({
        isshow: true
      })
    },
    onChange(e) {
      this.setData({
        timeData: e.detail,
      });
    },
    replaceText() {
      let xValue;
      xValue = 0;
      if (this.data.cloud_shop_info.master_status == 1) xValue += 1;
      if (this.data.cloud_shop_info.wechat_follow_status == 1) xValue += 1;
      if (this.data.cloud_shop_info.first_order_status == 0) xValue += 1;
      let {
        CDown_txt
      } = this.data.datas;
      // 使用正则表达式替换X
      CDown_txt = CDown_txt.replace(/X/, xValue.toString());
      CDown_txt = CDown_txt.replace(/Time/, "");
      // 更新数据
      // this.datas.CDown_txt = CDown_txt;
      this.setData({
        'datas.CDown_txt': CDown_txt
      })
    },
    getiPnoneBottom() {
      try {
        var value = wx.getStorageSync("iPhoneBottom");
        if (value) {
          this.setData({
            iPnoneBottomBol: true,
          });
        }
      } catch (e) {
        // Do something when catch error
      }
    },
    to_shop_diyPage() {
      if (this.data.cloud_shop_info.wechat_follow) return
      wx.navigateTo({
        url: '/mircoApp/cloud_shop/cloud_shop_diyPage/cloud_shop_diyPage',
      })
    },
    to_billing_Zone_first() {
      if (this.data.cloud_shop_info.first_order_status) return
      wx.navigateTo({
        url: '/mircoApp/cloud_shop/billing_Zone/billing_Zone',
      })
    },
    async get_shop_reward() {
      let ids = this.data.datas.list.map(ele => ele.id);
      let {
        data
      } = await app._postNetWork({
        url: app.getNetAddresss("plugin.cloud-shop.frontend.kaidan.index"),
        data: {
          goods_ids: ids
        }
      })
      if (data.result) {
        let arrData = data.data.list.data;
        const idToGoodsMap = {};
        arrData.forEach(item => {
          idToGoodsMap[item.id] = item.cloud_shop_reward_amount;
        });
        // 更新 this.datas.list 中对应 id 的数据
        this.data.datas.list.forEach(item => {
          if (idToGoodsMap[item.id]) {
            item['cloud_shop_reward_amount'] = idToGoodsMap[item.id]
          }
        });
        this.setData({
          newAccountShop: true,
          'datas.list': this.data.datas.list
        })
      } else {
        app.tips(data.msg);
      }
    },
    toDiyPage() {
      wx.navigateTo({
        url: '/mircoApp/cloud_shop/cloud_shop_diyPage/cloud_shop_diyPage',
      })
    },
    openStoreClose() {
      this.setData({
        openStore: false
      })
    },
    newAccount_open() {
      this.setData({
        newAccount: true
      })
    },
    newAccount_close() {
      this.setData({
        newAccount: false
      })
    },
    to_billing_Zone() {
      wx.navigateTo({
        url: '/mircoApp/cloud_shop/billing_Zone/billing_Zone',
      })
    },
    newAccountShop_close() {
      this.setData({
        newAccountShop: false
      })
    },
    onClose() {
      this.newAccountShop_close();
      this.newAccount_close();
      this.setData({
        newAccountTask: false
      })
    }
  }
})